package org.scboy.dbms.service.imp;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.scboy.coobi.sql.factory.EntityFactory;
import org.scboy.dbms.dao.BaseSystemConfigDao;
import org.scboy.dbms.loads.LoadSystemConfig;
import org.scboy.dbms.service.SystemConfigService;
import org.scboy.utils.entitys.coobi.Model;
import org.scboy.utils.entitys.coobi.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class SystemConfigServiceImp implements SystemConfigService {


	@Autowired
	EntityFactory factory;
	
	@Autowired
	LoadSystemConfig cache;
	
	@Override
	public Page page(Model model) {
		BaseSystemConfigDao dao=factory.getDao(BaseSystemConfigDao.class);
		model.orderBy("zindex");
		return dao.page(model);
	}

	@Override
	public boolean update(Model model) {
		BaseSystemConfigDao dao=factory.getDao(BaseSystemConfigDao.class);
		int resultCount = dao.update(model);
		if(resultCount>0){
			cache.updateByKey(model.getString("code"));
			return true;
		}else{
			return false;
		}
	}

	@Override
	public Model get(String code) {
		BaseSystemConfigDao dao=factory.getDao(BaseSystemConfigDao.class);
		return dao.get(Model.create().add("code", code));
	}

	@Override
	public Map<String, String> getSystemConfig(String appName) {
		
		BaseSystemConfigDao dao=factory.getDao(BaseSystemConfigDao.class);
		
		Map<String, String> result=new HashMap<String, String>();
 		List<Model> datas= dao.select(Model.create().add("appName", appName));
 		
 		for (Model config : datas) {
 			result.put(config.getString("code"), config.getString("configValue"));
		}
 		
 		return result;
	}

	@Override
	public Map<String, String> getSystemConfigAll() {
		
		BaseSystemConfigDao dao=factory.getDao(BaseSystemConfigDao.class);
		
		Map<String, String> result=new HashMap<String, String>();
 		List<Model> datas= dao.select(Model.create());
 		
 		for (Model config : datas) {
 			result.put(config.getString("code"), config.getString("configValue"));
		}
 		
 		return result;
	}
	
}
